package com.sth.wiki.controller;

import com.sth.wiki.req.EbookSaveReq;
import com.sth.wiki.resp.CommonResp;
import com.sth.wiki.req.EbookQueryReq;
import com.sth.wiki.resp.EbookQueryResp;
import com.sth.wiki.resp.PageResp;
import com.sth.wiki.service.EbookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sth http://127.0.0.1:8888/wiki/ebook/list
 * @since 2023-11-03
 */
@Api(tags = "电子书")
@RestController
@RequestMapping("/wiki/ebook")
public class EbookController {

    @Autowired
    private EbookService ebookService;

    /**
     *   @Valid 代表开始校验规则
     * @param req
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "电子书列表")
    private CommonResp list(@Valid EbookQueryReq req) {
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        PageResp<EbookQueryResp> list = ebookService.list(req);
        resp.setContent(list);
        return resp;
    }
    @PostMapping("/save")
    @ApiOperation(value = "电子书保存")
    public CommonResp save(@Valid @RequestBody EbookSaveReq req) {
        CommonResp resp = new CommonResp<>();
        ebookService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "电子书删除")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        ebookService.delete(id);
        return resp;
    }
}

